//
// Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
// Copyright (C) 2017 Google, Inc.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//    Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
//    Redistributions in binary form must reproduce the above
//    copyright notice, this list of conditions and the following
//    disclaimer in the documentation and/or other materials provided
//    with the distribution.
//
//    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//

#pragma once

#include <algorithm>
#include <fstream>
#include <string>
#include <vector>

#include <glslang/Public/ShaderLang.h>

namespace ed {
	// Default include class for normal include convention of search backward
	// through the stack of active include paths (for nested includes).
	// Can be overridden to customize.
	class HLSLFileIncluder : public glslang::TShader::Includer {
	public:
		HLSLFileIncluder()
				: externalLocalDirectoryCount(0)
		{
		}

		virtual IncludeResult* includeLocal(const char* headerName,
			const char* includerName,
			size_t inclusionDepth) override
		{
			return readLocalPath(headerName, includerName, (int)inclusionDepth);
		}

		virtual IncludeResult* includeSystem(const char* headerName,
			const char* /*includerName*/,
			size_t /*inclusionDepth*/) override
		{
			return readSystemPath(headerName);
		}

		// Externally set directories. E.g., from a command-line -I<dir>.
		//  - Most-recently pushed are checked first.
		//  - All these are checked after the parse-time stack of local directories
		//    is checked.
		//  - This only applies to the "local" form of #include.
		//  - Makes its own copy of the path.
		virtual void pushExternalLocalDirectory(const std::string& dir)
		{
			directoryStack.push_back(dir);
			externalLocalDirectoryCount = (int)directoryStack.size();
		}

		virtual void releaseInclude(IncludeResult* result) override
		{
			if (result != nullptr) {
				delete[] static_cast<tUserDataElement*>(result->userData);
				delete result;
			}
		}

		virtual ~HLSLFileIncluder() override { }

	protected:
		typedef char tUserDataElement;
		std::vector<std::string> directoryStack;
		int externalLocalDirectoryCount;

		// Search for a valid "local" path based on combining the stack of include
		// directories and the nominal name of the header.
		virtual IncludeResult* readLocalPath(const char* headerName, const char* includerName, int depth)
		{
			// Discard popped include directories, and
			// initialize when at parse-time first level.
			directoryStack.resize(depth + externalLocalDirectoryCount);
			if (depth == 1)
				directoryStack.back() = getDirectory(includerName);

			// Find a directory that works, using a reverse search of the include stack.
			for (auto it = directoryStack.rbegin(); it != directoryStack.rend(); ++it) {
				std::string path = *it + '/' + headerName;
				std::replace(path.begin(), path.end(), '\\', '/');
				std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
				if (file) {
					directoryStack.push_back(getDirectory(path));
					return newIncludeResult(path, file, (int)file.tellg());
				}
			}

			return nullptr;
		}

		// Search for a valid <system> path.
		// Not implemented yet; returning nullptr signals failure to find.
		virtual IncludeResult* readSystemPath(const char* headerName)
		{
			return this->readLocalPath(headerName, "", 1);
		}

		// Do actual reading of the file, filling in a new include result.
		virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const
		{
			char* content = new tUserDataElement[length];
			file.seekg(0, file.beg);
			file.read(content, length);
			return new IncludeResult(path, content, length, content);
		}

		// If no path markers, return current working directory.
		// Otherwise, strip file name and return path leading up to it.
		virtual std::string getDirectory(const std::string path) const
		{
			size_t last = path.find_last_of("/\\");
			return last == std::string::npos ? "." : path.substr(0, last);
		}
	};
}